# run_tests.py
import unittest
import sys
import os
import io

# 设置UTF-8输出环境
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

def run_all_tests():
    """运行所有测试"""
    print("🧪 开始运行雷达仿真智能体系统测试...")
    print("=" * 60)
    
    # 添加项目根目录到Python路径
    sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
    
    # 发现并运行所有测试
    test_loader = unittest.TestLoader()
    test_suite = test_loader.discover('tests', pattern='test_*.py')
    
    # 运行测试
    test_runner = unittest.TextTestRunner(verbosity=2)
    result = test_runner.run(test_suite)
    
    # 输出测试结果总结
    print("\n" + "=" * 60)
    print("🧪 测试结果总结:")
    print(f"✅ 成功测试: {result.testsRun - len(result.failures) - len(result.errors)}")
    print(f"❌ 失败测试: {len(result.failures)}")
    print(f"⚠️  错误测试: {len(result.errors)}")
    print(f"📊 总计测试: {result.testsRun}")
    
    if result.failures:
        print("\n❌ 失败的测试:")
        for test, traceback in result.failures:
            print(f"  - {test}: {traceback.split('AssertionError:')[-1].strip()}")
    
    if result.errors:
        print("\n⚠️ 错误的测试:")
        for test, traceback in result.errors:
            print(f"  - {test}: {traceback.split('Exception:')[-1].strip()}")
    
    print("=" * 60)
    
    if result.wasSuccessful():
        print("🎉 所有测试通过！系统功能验证成功！")
        return True
    else:
        print("⚠️ 部分测试失败，请检查代码实现")
        return False

if __name__ == '__main__':
    success = run_all_tests()
    sys.exit(0 if success else 1)